package com.xyoto.community.config;

import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.mysql.cj.jdbc.MysqlXADataSource;
import com.xyoto.community.config.property.XyotoLoveConfigProperties;
import com.xyoto.community.utils.handler.MyBatisHandle;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.xyoto.community.mapper.love", sqlSessionFactoryRef = "xyotoLoveSqlSessionFactory")
@Slf4j
@Import({XyotoLoveConfigProperties.class})
public class XyotoLoveConfig {

    @Autowired
    private MyBatisHandle myBatisHandle;

    @Autowired
    private XyotoLoveConfigProperties xyotoLoveConfigProperties;

    @Bean(name = "xyotoLoveDataSource")
    public DataSource xyotoLoveDataSource(){
        log.info("开始初始化【xyoto_love】数据源");
        // 新建一个支持XA协议的数据源
        MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
        // 设置数据库信息
        mysqlXADataSource.setURL(xyotoLoveConfigProperties.getUrl());
        mysqlXADataSource.setUser(xyotoLoveConfigProperties.getUsername());
        mysqlXADataSource.setPassword(xyotoLoveConfigProperties.getPassword());
        // 把创建好的数据源对象交给atomikos管理
        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
        atomikosDataSourceBean.setXaDataSource(mysqlXADataSource);
        // 设置资源标识
        atomikosDataSourceBean.setUniqueResourceName(xyotoLoveConfigProperties.getUniqueResourceName());
        log.info("完成初始化【xyoto_love】数据源");
        return atomikosDataSourceBean;
    }

    @Bean(name = "xyotoLoveSqlSessionFactory")
    public SqlSessionFactory xyotoLoveSqlSessionFactory(@Qualifier("xyotoLoveDataSource") DataSource dataSource) throws Exception {

        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/love/**/**/*.xml")
        );
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
        bean.setConfiguration(configuration);
        bean.setPlugins(myBatisHandle);

        return bean.getObject();
    }

    @Bean(name = "xyotoLoveSqlSessionTemplate")
    public SqlSessionTemplate xyotoLoveSqlSessionTemplate(@Qualifier("xyotoLoveSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}
